#!/bin/bash
#
#********************************************************************
#Author:                songliangcheng
#QQ:                    2192383945
#Date:                  2023-02-23 15:03:49
#FileName：             install-python.sh
#URL:                   http://blog.mykernel.cn
#Description：          https://gitee.com/slcnx/tools/raw/master/scripts/python/install-python.sh
#Copyright (C):        2023 All rights reserved
#********************************************************************
#描述:
#--version 选项必须传递
source <(curl -sSLf https://gitee.com/slcnx/tools/raw/master/parse_cmd.sh | sed 's/\r//g')

CONFIG='

key    ,argument    ,isempty    ,desc    

--version    ,VERSION    ,0    ,python版本
--script-http-proxy    ,_HTTP_PROXY    ,0    ,script级 http代理
--script-no-proxy    ,_NO_PROXY    ,1    ,script级 哪些IP可以不代理，给127.0.0.1,localhost追加
'

parse_cmd $@
export _HTTP_PROXY=$_HTTP_PROXY
export _NO_PROXY=$_NO_PROXY

function install_pkgs() {
  sed -i 's@mirrors.aliyun.com@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list
  apt update
  apt-get install build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev libncurses5-dev libncursesw5-dev xz-utils tk-dev liblzma-dev libmysqlclient-dev -y
}


function install_py() {
  source <(curl -sSLf https://gitee.com/slcnx/tools/raw/master/scripts/proxy/addosproxy.sh)
  tmpproxy $_HTTP_PROXY $_NO_PROXY

  curl https://pyenv.run | bash

  echo '
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
  ' > /etc/profile.d/Z99-pyenv.sh
  source /etc/profile.d/Z99-pyenv.sh
  pyenv install -v  $VERSION
  removeproxy
}


main() {
  install_pkgs
  install_py
}

main
